package com.origin.app.app_list.adapter

import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.ViewGroup
import com.origin.app.R
import com.origin.app.app_list.bean.AppInfo
import com.origin.app.databinding.ItemAppInfoBinding
import com.origin.uilibrary.SelectorFactory
import com.origin.uilibrary.addClickScale
import com.origin.uilibrary.xrecycleview.adapter.BaseRecycleAdapter
import com.origin.uilibrary.xrecycleview.viewholder.BaseViewHolder

class AppAdapter(list: MutableList<AppInfo>, context: Context) :
    BaseRecycleAdapter<AppInfo>(list, context) {
    private val itemBg = SelectorFactory.newShapeSelector().setCornerRadius(20)
        .setPressedBgColor(Color.parseColor("#cccccc"))
        .setDefaultBgColor(
            Color.parseColor("#000000")
        );
    private val paddingDistance = 16;

    override fun setItemData(holder: BaseViewHolder, item: AppInfo, position: Int) {
        val itemAppInfoBinding = holder.binding as ItemAppInfoBinding
        itemAppInfoBinding.appInfo = item
        if (position % 2 == 0) {
            itemAppInfoBinding.root.setPadding(
                paddingDistance,
                paddingDistance,
                paddingDistance / 2,
                paddingDistance
            )
        } else {
            itemAppInfoBinding.root.setPadding(
                paddingDistance / 2,
                paddingDistance,
                paddingDistance,
                paddingDistance
            )
        }
        itemAppInfoBinding.box.background = itemBg.create()
        holder.itemView.addClickScale()
    }

    override fun getViewResource(
        inflater: LayoutInflater,
        viewType: Int,
        parent: ViewGroup
    ): BaseViewHolder = BaseViewHolder(
        loadDataBinding<ItemAppInfoBinding>(
            inflater,
            parent,
            R.layout.item_app_info
        )
    )
}